var settings = settings ||{};
var pageContext = new PageContext(settings||{});
pageContext.back = function(target){
	var $lis = $("#page-header>ul>li");
	var len = $lis.length ;
	$($lis.get(len-2)).find("a").click();
}

pageContext.addItemAfter = function(result,para){
	pageContext.back();
};
pageContext.updateItemAfter = function(result,para){
	pageContext.back();
};
var searchTreesetting = {
		view: {
			dblClickExpand: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			 onClick: onInputTreeClickSearch,
			 beforeClick: zTreeBeforeClick
		}
}; 

var treeSetting = {
		dialog:false
		,onInputTreeBodyDown:onInputTreeBodyDown
		,target:$("#applyeditForm").find("input[name='tableId']").next("input")
}
var pageContextInputTree = new PageContextInputTree(treeSetting);

function onInputTreeClickSearch(e, treeId, treeNode) {
	if(treeNode.type!="table"){
		return false;
	}
	pageContextInputTree.onClickSearch(e, treeId, treeNode);
	var target = $("#applyeditForm").find("select[name='fieldId']");
	try{
		target.select2("destroy")
	}catch(e){}
	 $.get(window.global.ctx+"/str/data/fields?noPage=1",{id:treeNode.id},function(result){
		    var data = result.data;
			if(data){ 
				var list = data;
				target.empty();
				target.append("<option value=''>--字段--</option>")
				for(index in list){
					var f = list[index];
					if(false==f.sysClass){
						target.append("<option value='"+f.id+"'>"+f.nameCn+"</option>")
					}
				}
				target.select2({language: "zh-CN",placeholder: "字段"})
			}
	 })
}
function zTreeBeforeClick(treeId, treeNode, clickFlag) {
    return (treeNode.type=="table");
};

function onInputTreeBodyDown(event) {
	
	var node = $(event.target).closest("li").attr("treenode");
	if (!($(event.target).attr("treehide") || node=="")) {
		pageContextInputTree.hideMenu();
	}
} 
jQuery(function($) {
	 $(document).find("input").each(function(){
		 $(this).attr("autocomplete","off");
	 });
	pageContext.init();
	var layero = $("#applyeditForm");
	layero.find('select').each(function(){
		var me = $(this);
		me.val(me.attr("oldval"))
	  });
	layero.find('select[novalid!="true"]').each(function(){
		var novalid = $(this).attr("noconvert");
		if(novalid!="true"){
			layero.find(this).select2({
	            language: "zh-CN",width:"off"})
	            .next("span").removeClass("col-xs-10-select2").addClass("col-xs-10-select2");
		}
	  });
	layero.find('.num').each(function(){
		var me = $(this);
		var min = me.attr("min");
		if(min==undefined||min==""){
			min = 0;
		}
		me.ace_spinner({value:me.attr("oldval"),min:min,step:1, on_sides: true, icon_up:'ace-icon fa fa-plus bigger-110', icon_down:'ace-icon fa fa-minus bigger-110', btn_up_class:'btn-success' , btn_down_class:'btn-danger'});
	  });
	 $.get(window.global.ctx+"/str/table/tree?noPage=1",{},function(result){
		    var data = result;
			if(data){ 
				var list = data;
				pageContextInputTree.initTree($($("#applyeditForm").find(".ztree")[0]), list,searchTreesetting);
			}
	 })
}); 
